import Singleton from "../base/Singleton";
import { BUNDLE_TYPE_ENUM, GAME_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { AdManager } from "../ads/AdManager";
/**
 * 加载资源管理器
 */
export class ResManager1 extends Singleton {
    static get ins() {
        return super.getInstance<ResManager1>();
    }

    /** homeAssets 主要包括关卡外用到的资源，如主页界面，关卡界面等*/
    private homeBundle: cc.AssetManager.Bundle = null;
    /** homeAssets马甲包 ui资源homeBundle、马甲包单独资源在homeBundleExtra*/
    private homeBundleExtra: cc.AssetManager.Bundle = null;
    /** gameAssets 主要包括关卡内通用资源，如胜利界面，失败界面等*/
    private gameBundle: cc.AssetManager.Bundle = null;
    /** 每关的bundle */
    private levelBundle: cc.AssetManager.Bundle = null;
    /** 每关的截图bundle */
    private levelSprBundle: cc.AssetManager.Bundle = null;

    private gameType: GAME_TYPE_ENUM = GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN;

    init() {
        this.gameType = window["selectGame"];
    }
    /** 加载游戏主包用到的资源bundle */
    async loadHomeAssetsBundle() {
        if (!this.homeBundle) {
            //乱斗大比拼、脑力勇者、最强折磨用的文字我服了资源
            switch (this.gameType) {
                case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
                case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
                case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
                    this.homeBundle = await this.loadBundle("HomeAssets_" + GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE);
                    this.homeBundleExtra = await this.loadBundle("HomeAssets_" + this.gameType);
                    break;
                default:
                    this.homeBundle = await this.loadBundle("HomeAssets_" + this.gameType);
                    break;
            }
        }
    }
    /** 加载关卡内用到的资源bundle */
    async loadLevelSprBundle() {
        if (!this.levelSprBundle)
            this.levelSprBundle = await this.loadBundle("bundleLevelSpr");
    }
    /** 加载关卡内用到的资源bundle */
    async loadGameAssetsBundle() {
        if (!this.gameBundle) {
            //乱斗大比拼、脑力勇者、最强折磨用的文字我服了资源
            switch (this.gameType) {
                case GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE:
                case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
                case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
                case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
                    this.gameBundle = await this.loadBundle("GameAssets_" + GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE);
                    break;
                default:
                    this.gameBundle = await this.loadBundle("GameAssets_" + this.gameType);
                    break;
            }
        }
    }

    /** 加载关卡bundle */
    async loadLevelBundle(bundleName: string) {
        /** 加载下一关bundle时释放上一关bundle */
        if (this.levelBundle && this.levelBundle.name != bundleName) {
            this.releaseLevelBundle();
        }
        let realBundleName = this.getBundleName(bundleName);
        this.levelBundle = await this.loadBundle(realBundleName);
    }
    /** 释放关卡内bundle */
    releaseLevelBundle() {
        if (!this.levelBundle) return;
        console.log("release " + this.levelBundle.name);
        this.levelBundle.releaseAll();
        cc.assetManager.removeBundle(this.levelBundle);
        this.levelBundle = null;
    }

    /** 加载bundle */
    loadBundle(bundleName: string) {
        return new Promise<cc.AssetManager.Bundle>((resolve, reason) => {
            cc.assetManager.loadBundle(bundleName, (err, bundle) => {
                if (err) {
                    reason(err);
                    return;
                }
                resolve(bundle);
            });
        })
    }
    /** 预加载资源 */
    preloadRes(path: string | string[], type: typeof cc.Asset, bundleType: BUNDLE_TYPE_ENUM = BUNDLE_TYPE_ENUM.MAIN) {
        let bundle = this.getBundleByType(bundleType);
        if (!bundle) bundle = cc.resources;
        bundle.preload(path, type)
    }
    /** 加载单个资源 */
    loadRes<T extends cc.Asset>(path: string, type: { prototype: T }, bundleType: BUNDLE_TYPE_ENUM = BUNDLE_TYPE_ENUM.MAIN) {
        let bundle = this.getBundleByType(bundleType);
        if (!bundle) bundle = cc.resources;
        // console.log("loadRes", path, bundle.name)
        return new Promise<T>((resolve, reject) => {
            bundle.load(path, type, (err, res) => {
                if (err) {
                    reject(err);
                    return
                }
                resolve(res);
            });
        });
    }

    /** 根据bundleType 获取bundle */
    private getBundleByType(bundleType: BUNDLE_TYPE_ENUM) {
        switch (bundleType) {
            case BUNDLE_TYPE_ENUM.MAIN:
                return cc.resources;
            case BUNDLE_TYPE_ENUM.HOME_EXTRA:
                //乱斗大比拼、脑力勇者、最强折磨用的文字我服了资源
                switch (this.gameType) {
                    case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
                    case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
                    case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
                        return this.homeBundleExtra;
                    default:
                        return this.homeBundle;
                }
            case BUNDLE_TYPE_ENUM.HOME:
                return this.homeBundle;
            case BUNDLE_TYPE_ENUM.GAME:
                return this.gameBundle;
            case BUNDLE_TYPE_ENUM.LEVEL:
                return this.levelBundle;
            case BUNDLE_TYPE_ENUM.LEVEL_SPR:
                return this.levelSprBundle;
            default:
                return cc.resources;
        }
    }

    /** 获取实际的Bundle url */
    private getBundleName(bundleName: string) {
        let realBundleName = bundleName;
        // 通过打包插件设置是否是本地测试
        let releaseType = window["local"] ? RELEASE_TYPE_ENUM.local : AdManager.releaseType;
        switch (releaseType) {
            /**本地测试 与 安卓  暂不使用远端bundle */
            case RELEASE_TYPE_ENUM.local:
            case RELEASE_TYPE_ENUM.android:
            case RELEASE_TYPE_ENUM.android_233:
            case RELEASE_TYPE_ENUM.h5_4399:
                break;
            default:
                let urlPrefix = '';
                switch (this.gameType) {
                    case GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN:
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/KaiShiNiXiBa/';
                        break;
                    case GAME_TYPE_ENUM.WO_YAO_BIAN_TU_LE:
                        // urlPrefix = 'https://naodthbdl.xmqixiang.cn/';
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoHuiLaoJia/';
                        break;
                    case GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE:
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/';
                        if (AdManager.gameId === 14 || AdManager.gameId === 8) {
                            urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ZhengHuoDaHangJia/';
                        }
                        break;
                    case GAME_TYPE_ENUM.WAN_DAN_ZHANG_NAO_ZI_LE:
                        // urlPrefix = 'https://naodthbdl.xmqixiang.cn/WanDanZhangNaoZiLe/';
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/YongShiZhanDouBa/';
                        break;
                    case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
                        urlPrefix = 'https://res.wqop2018.com/partner/sjzsl/lddbp/';
                        break;
                    case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/zuiQiangZheMo/';
                        break;
                    case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
                        urlPrefix = 'https://nlyz-cdn.jinkezhexin.top/';
                        break;
                    default:
                        urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/NaoDongTianHuaBan/';
                        break;
                }
                let url = urlPrefix + releaseType + "/levelBundle/";
                // if (releaseType === RELEASE_TYPE_ENUM.ks && this.gameType === GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE) {
                //     // url = 'https://cdngame.shuling.vip/BianJuDaShi/ZhengHuoDaHangJia/ksgame_wenfu/levelBundle/';
                //     url = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ksgame/levelBundle/';
                // } else 
                if (AdManager.gameId === 18) {
                    // url = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoBianTuLe/ksgame/levelBundle/';
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/ZheBoWoHenXiu/ksgame/levelBundle/';
                } else if (AdManager.gameId === 26) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/XieYinGengZhuan/ksgame/levelBundle/';
                } else if (AdManager.gameId === 23) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoShangAn/ksgame/levelBundle/';
                } else if (AdManager.gameId === 25) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/XieYinDaShi/ksgame/levelBundle/';
                } else if (AdManager.gameId === 29) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/NaoDongTianHuaBan/ksgame/levelBundle/';
                } else if (AdManager.gameId === 30) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/ChaoCaiWoZuiKuang/ksgame/levelBundle/';
                }else if (AdManager.gameId === 40) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/NaoLiXiuQiLai/ksgame/levelBundle2/';
                }else if (AdManager.gameId === 13) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ZhengHuoDaHangJia/bytedance/levelBundle/';
                }else if (AdManager.gameId === 42) {
                    url = 'https://cdngame.shuling.vip/BianJuDaShi/KanWoLaiZhengHuo/ksgame/levelBundle2/';
                }
                realBundleName = url + bundleName;
                break;
        }
        return realBundleName;
    };
}